#include "affichage.h"
#include <stdio.h>
#include <stdlib.h>
#include "actions.h"
#include <unistd.h>
#include <string.h>

void Accueil(int *reglages, int *AccordSelected,char** notes_latin, char** notes_ang)
{
    int notation;
    int affichage;
    int quit = 1,c;
    
    while(quit==1)
    {
        system("CLS");
        printf("|******** Baladeur eX++ Welcome ! ********|\n\n");
        printf("1- Reglages des options\n");
        printf("2- Mode Piano\n");
        printf("3- Mode Guitare\n");
        printf("0- Quitter\n\n");
        
        printf("-> ");
        scanf("%d", &c);

    switch (c)
    {
        case 1:
                Reglages(reglages);
                notation = reglages[0];
                affichage = reglages[1];
        break;
        case 2:
               Piano(AccordSelected, notation, affichage, notes_latin, notes_ang);
        break;
        case 3:
               Guitare(AccordSelected, notation, affichage, notes_latin, notes_ang);
        break;
        case 0:
                quit=0;
        break;
        default:
                printf("Veuillez retenter votre chance!\n");
    }
    }
}

void Reglages(int *reglages)
{
    int quit;
    int c;
    quit=1;

    while(quit==1)
    {
        system("CLS");
        printf("1- Types de notation\n");
        printf("2- Types affichage\n");
        printf("0- Sortir du menu Reglages\n\n");
        //printf("Actuellement : \n Notation : %d \n Affichage = %d\n", notation,affichage);  //Affichage pour debugguer ces deux variables
       
        printf("-> ");
        scanf("%d", &c);
       
        switch (c)
        {
                case 1:
                         reglages[0] = MenuNotation();
                         
                break;

                case 2:
                         reglages[1] = MenuAffichage();
                break;

                case 0:
                         quit=0;
                break;
                default:
                        printf("Veuillez retenter votre chance!\n");
                        system("PAUSE");
        }
    }
}


int AffichageGamme(int notation,char **notes_latin,char **notes_ang,int Fondamentale)
{
    int Gamme;
    
    system("CLS");
    
    if(notation == 1)
    {
    printf("Selection de la gamme pour le : %s \n\n",notes_latin[Fondamentale]);
    }
    else if(notation == 2)
    {
        printf("Selection de la gamme pour le : %s \n\n",notes_ang[Fondamentale]);
    }
    printf("1- Mineur\n");
    printf("2- Majeur\n");
    printf("3- 7eme\n");
    printf("4- 7eme Mineur\n\n");
    
    printf("-> ");
    scanf("%d",&Gamme);
    
    return Gamme;
   
}

int MenuNotation(void)
{
    int quit;
    int c;
    
    quit=1;

    while(quit==1)
    {
        system("CLS");
        
        printf("1- Numerotation latine (Do,Re,Mi ...)\n");
        printf("2- Numerotation anglo-saxonne (A,B,C ...)\n");
        printf("0- Retourner au menu Reglages\n\n");
        
        printf("-> ");
        scanf("%d", &c);

        switch (c)
        {
                case 1:
                        return 1;
                break;
                case 2:
                        return 2;
                break;
                case 0:
                        quit=0;
                break;

                default: printf("Veuillez retenter votre chance!\n");
                         system("PAUSE");
        }
    }
}

int MenuAffichage(void)
{
    int quit;
    int c;

    quit=1;
    
    while(quit==1)
    {
        system("CLS");
        printf("1- Affichage complet(Affichage des notes)\n");
        printf("2- Affichage simple(Seules les positions des doigts sont affichees)\n");
        printf("0- Retourner au menu Reglages\n\n");
        
        printf("-> ");
        scanf("%d", &c);

        switch (c)
        {
                case 1:
                        return 1;
                break;
                case 2:
                        return 2;
                break;
                case 0:
                        quit=0;
                break;
                default: printf("Veuillez retenter votre chance!\n");
                         system("PAUSE");
        }
    }
}

void AffichageClavierPiano(int *AccordSelected,int Notation,int Affichage)
{
    int i,j;
    int pos;
    int size;
    char piano[10][57]=
                        {{218,196,196,220,220,220,196,220,220,220,196,220,220,220,196,196,194,196,196,220,220,220,196,220,220,220,196,196,194,196,196,220,220,220,196,220,220,220,196,220,220,220,196,196,194,196,196,220,220,220,196,220,220,220,196,196,191},
                        {179,255,255,219,219,219,255,219,219,219,255,219,219,219,255,255,179,255,255,219,219,219,255,219,219,219,255,255,179,255,255,219,219,219,255,219,219,219,255,219,219,219,255,255,179,255,255,219,219,219,255,219,219,219,255,255,179},
                        {179,255,255,219,219,219,255,219,219,219,255,219,219,219,255,255,179,255,255,219,219,219,255,219,219,219,255,255,179,255,255,219,219,219,255,219,219,219,255,219,219,219,255,255,179,255,255,219,219,219,255,219,219,219,255,255,179},
                        {179,255,255,219,219,219,255,219,219,219,255,219,219,219,255,255,179,255,255,219,219,219,255,219,219,219,255,255,179,255,255,219,219,219,255,219,219,219,255,219,219,219,255,255,179,255,255,219,219,219,255,219,219,219,255,255,179},
                        {179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179},
                        {179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179},
                        {179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179},
                        {179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179,255,255,255,179},
                        {192,196,196,196,193,196,196,196,193,196,196,196,193,196,196,196,193,196,196,196,193,196,196,196,193,196,196,196,193,196,196,196,193,196,196,196,193,196,196,196,193,196,196,196,193,196,196,196,193,196,196,196,193,196,196,196,217}};
    
    if(AccordSelected[5] == 1 || AccordSelected[5] == 2)
    {
        size = 3;
    }
    else if(AccordSelected[5] == 3 || AccordSelected[5] == 4)
    {
        size = 4;
    }
   for(i=0;i<size;i++)
   {
       if((AccordSelected[i]) <= 4)
       {
        if((AccordSelected[i] % 2) == 0)
        {
                //printf("%d",AccordSelected[i]);  //debug line
                pos = 18 + (AccordSelected[i]/2) * 4;
                piano[5][pos] = 177;
        }
        else
        {
                        pos = 20 + (AccordSelected[i]/2) * 4;
                        piano[3][pos] = 177;

        }
       }else{
        if((AccordSelected[i] % 2) == 0)
        {
                //printf("%d",AccordSelected[i]);  //debug line
                pos = 20 + (AccordSelected[i]/2) * 4;
                piano[3][pos] = 177;
        }
        else
        {
                        pos = 22 + (AccordSelected[i]/2) * 4;
                        piano[5][pos] = 177;
                
        }
       }
   }
   for(i=0;i<10;i++)
   {
        for(j=0;j<57;j++)
        {
                printf("%c",piano[i][j]);
        }
        
        printf("\n");
   }
}

void AffichageClavierGuitare(int *AccordSelected, int Notation, int Affichage, char** notes_ang)
{
    //On doit ici constituer une chaine de la forme NoteGamme que j'enregistre dans AccordGamme pour la comparer ligne 307
    
    char *AccordGamme;
    AccordGamme = malloc(sizeof(char)*4);
    AccordGamme[0] = notes_ang[AccordSelected[0]][0];
    
     
   // Assignation char avec le int qu'on reçoit
    
    switch (AccordSelected[5])
    {
            case 1 : AccordGamme[1] = 'm';
            break;
            case 2 : AccordGamme[1] = 'M';
            break;
            case 3 : AccordGamme[1] = '7';
            break;
            case 4 : AccordGamme[1] = '7m';
            break;
    }

    //On signale que la chaine prend fin a la 3eme case
    AccordGamme[2] = '\0';
    
    //Titre
    printf("Les positions pour l'accord de %s sont :\n",AccordGamme);
    
    //Declaration chaine
    R* l1 = NULL;

    //Déclaration de toutes les variables dont on a besoin
    int i,j,barre = 0,size,note_ref = 0;
    
    char doights_cor[] = {'i','m','a','r'};
    int doights_num[4];

    int decal = 0,ligne,colonne;
    
    int **guitare = NULL;
    
    int guitare_temp[17][57]=
       {{255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 255,255,255 ,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 255,255,255 ,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255},
        {201,205,205,203,205,205,203,205,205,203,205,205,203,205,205,187, 255,255,255 ,201,205,205,203,205,205,203,205,205,203,205,205,203,205,205,187, 255,255,255 ,201,205,205,203,205,205,203,205,205,203,205,205,203,205,205,187,255,255,255},
        {179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,179, 255,255,255 ,179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,179, 255,255,255 ,179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,255},
        {179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,179, 255,255,255 ,179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,179, 255,255,255 ,179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,255},
        {195,196,196,197,196,196,197,196,196,197,196,196,197,196,196,180, 255,255,255 ,195,196,196,197,196,196,197,196,196,197,196,196,197,196,196,180, 255,255,255 ,195,196,196,197,196,196,197,196,196,197,196,196,197,196,196,180,255,255,255},
        {179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,179, 255,255,255 ,179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,179, 255,255,255 ,179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,255},
        {179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,179, 255,255,255 ,179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,179, 255,255,255 ,179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,255},
        {195,196,196,197,196,196,197,196,196,197,196,196,197,196,196,180, 255,255,255 ,195,196,196,197,196,196,197,196,196,197,196,196,197,196,196,180, 255,255,255 ,195,196,196,197,196,196,197,196,196,197,196,196,197,196,196,180,255,255,255},
        {179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,179, 255,255,255 ,179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,179, 255,255,255 ,179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,255},
        {179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,179, 255,255,255 ,179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,179, 255,255,255 ,179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,255},
        {195,196,196,197,196,196,197,196,196,197,196,196,197,196,196,180, 255,255,255 ,195,196,196,197,196,196,197,196,196,197,196,196,197,196,196,180, 255,255,255 ,195,196,196,197,196,196,197,196,196,197,196,196,197,196,196,180,255,255,255},
        {179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,179, 255,255,255 ,179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,179, 255,255,255 ,179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,255},
        {179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,179, 255,255,255 ,179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,179, 255,255,255 ,179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,255},
        {195,196,196,197,196,196,197,196,196,197,196,196,197,196,196,180, 255,255,255 ,195,196,196,197,196,196,197,196,196,197,196,196,197,196,196,180, 255,255,255 ,195,196,196,197,196,196,197,196,196,197,196,196,197,196,196,180,255,255,255},
        {179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,179, 255,255,255 ,179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,179, 255,255,255 ,179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,255},
        {179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,179, 255,255,255 ,179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,179, 255,255,255 ,179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,179,255,255,255},
        {192,196,196,193,196,196,193,196,196,193,196,196,193,196,196,217, 255,255,255 ,192,196,196,193,196,196,193,196,196,193,196,196,193,196,196,217, 255,255,255 ,192,196,196,193,196,196,193,196,196,193,196,196,193,196,196,217,255,255,255}};
       
    
    char *accord1;
    char *accord2;
    char *accord3;

    int * cases1;
    int * cases2;
    int * cases3;

    char *doights1;
    char *doights2;
    char *doights3;

    //Allocation de mémoire
    accord1 = malloc(sizeof(char) * 4);
    cases1 = malloc(sizeof(int) * 6);
    doights1 = malloc(sizeof(char) * 7);

    accord2 = malloc(sizeof(char) * 4);
    cases2 = malloc(sizeof(int) * 6);
    doights2 = malloc(sizeof(char) * 7);

    accord3 = malloc(sizeof(char) * 4);
    cases3 = malloc(sizeof(int) * 6);
    doights3 = malloc(sizeof(char) * 7);
    
    guitare = malloc(sizeof(int *) * 17);
    
    for(i = 0; i < 17; i++)
    {
        guitare[i] = malloc(sizeof(int) * 57);
    }
    
        for(i=0;i<17;i++)
        {
                for(j=0;j<57;j++)
                {
                        guitare[i][j]=guitare_temp[i][j];
                }
        }

    //On signale au programme que accord3 peut etre non assignée
    accord3[0] = '\0';
    
    //Récupération de la liste
    l1 = chaine(l1);

    //Traitement de la liste chainée
    i = 0;
    while(l1)
    {
        if(strcmp(l1->accord,AccordGamme) == 0)
        {
            i++;
            switch (i)
            {
                
                case 1 :
                         accord1 = l1->accord;
                        cases1 = l1->cases;
                        doights1 = l1->doigts;
                break;
                case 2 :
                        accord2 = l1->accord;
                        cases2 = l1->cases;
                        doights2 = l1->doigts;
                break;
                case 3 :
                        accord3 = l1->accord;
                        cases3 = l1->cases;
                        doights3 = l1->doigts;
                break;
            }
            /*
            printf("%s\n", l1->accord);
            printf("%d:", l1->cases);
            printf("%s\n", l1->doigts);
            */
        }

        l1=l1->next;
    }
    
        //Appel des fonctions de calcul
        calcul_guitare(1,accord1,cases1,doights1,guitare);
        calcul_guitare(2,accord2,cases2,doights2,guitare);
        
        if(accord3[0] != '\0')
        {
                calcul_guitare(3,accord3,cases3,doights3,guitare);
        }
    //Affichage du nom de l'accord
    printf("       %s                 %s                 %s         \n",accord1,accord2,accord3);

    //Premier accord
    for(i=0;i<17;i++)
    {
        for(j=0;j<57;j++)
        {
            if(guitare[i][j] > 50)
            {
                printf("%c",guitare[i][j]);
            }
            else
            {
                printf("%d",guitare[i][j]);
            }
        }
        printf("\n");
 
    }
    /* // focntion d'affichage non terminée
    for(j=0;j<6;j++)
        {
            if(cases1[i] != 'x')
            {
                printf("%s|",cases1[i]+i);
            }
        }
     */
}

